﻿/*
=======================================================
    UsingWirerope【在用绳档案】
=======================================================
*/

using Entities;

namespace Services
{
    public interface IUsingWireropeRepository : IRepository<UsingWirerope>
    {
        void InsertForAdmin(UsingWirerope usingWirerope, Bill bill, BillDetail billDetail, Stock stockInfo);
    }


    public class UsingWireropeRepository : BaseRepository<UsingWirerope>, IUsingWireropeRepository
    {
        public UsingWireropeRepository(ISqlSugarClient dB, ICurrentUser currentUser, IQuery<UsingWirerope> query)
            : base(dB, currentUser, query)
        {
        }

        #region InsertForAdmin
        public void InsertForAdmin(UsingWirerope usingWirerope, Bill bill, BillDetail billDetail, Stock stockInfo)
        {
            Insert(usingWirerope);

            var billID = _DB.Insertable(bill).ExecuteReturnIdentity();

            billDetail.BillID = billID;

            _DB.Insertable(billDetail).ExecuteCommand();

            _DB.Updateable(stockInfo).ExecuteCommand();
        } 
        #endregion

        #region GetPage
        public override (List<UsingWirerope> items, int totalCount) GetPage(Expressionable<UsingWirerope> where, int pageIndex = 1, int pageSize = 20, string orderBy = null)
        {
            var fs = _DB.Queryable<UsingWirerope, Department>
                 ((x, d) => new JoinQueryInfos(
                    JoinType.Inner, x.DepartmentID == d.DepartmentID
                 ))
                 .Select((x, d) => new UsingWirerope
                 {
                     UsingWireropeID = x.UsingWireropeID.SelectAll(),
                     Department_Name = d.Name
                 });

            return _query.GetPage(where, pageIndex, pageSize, orderBy, fs);
        }
        #endregion

        #region GetModel
        public override UsingWirerope GetModel(Expressionable<UsingWirerope> where, string orderBy = null)
        {
            var fs = _DB.Queryable<UsingWirerope, Department>
                 ((x, d) => new JoinQueryInfos(
                    JoinType.Inner, x.DepartmentID == d.DepartmentID
                 ))
                 .Select((x, d) => new UsingWirerope
                 {
                     UsingWireropeID = x.UsingWireropeID.SelectAll(),
                     Department_Name = d.Name
                 });

            return _query.GetModel(where, orderBy, fs);
        }
        #endregion

        #region GetSearch
        public override List<UsingWirerope> GetSearch(Expressionable<UsingWirerope> where, string orderBy = null, int? top = null)
        {
            var fs = _DB.Queryable<UsingWirerope, Department>
                 ((x, d) => new JoinQueryInfos(
                    JoinType.Inner, x.DepartmentID == d.DepartmentID
                 ))
                 .Select((x, d) => new UsingWirerope
                 {
                     UsingWireropeID = x.UsingWireropeID.SelectAll(),
                     Department_Name = d.Name
                 });

            return _query.GetSearch(where, orderBy, top, fs);
        }
        #endregion
    }
}
